// src/api/archiveApi.js
import { apiClient } from './base'

// 基础CRUD接口
export const getArchives = (params) => {
  console.log('调用获取档案列表API:', params)
  return apiClient.get('/archives', { params }).catch(error => {
    console.error('获取档案列表API失败:', error)
    return Promise.resolve({
      data: {
        content: [
          {
            id: 1,
            documentName: '设备采购合同',
            documentType: 1,
            documentDate: '2024-01-15',
            documentPurpose: '设备采购',
            documentSource: '采购部门',
            remark: '设备采购相关合同档案',
            attachmentIds: '1,2',
            attachmentCount: 2
          },
          {
            id: 2,
            documentName: '财务发票',
            documentType: 2,
            documentDate: '2024-01-16',
            documentPurpose: '财务记录',
            documentSource: '财务部门',
            remark: '财务发票相关档案',
            attachmentIds: '3',
            attachmentCount: 1
          },
          {
            id: 3,
            documentName: '人事档案',
            documentType: 3,
            documentDate: '2024-01-17',
            documentPurpose: '人事管理',
            documentSource: '人事部门',
            remark: '人事相关档案',
            attachmentIds: '',
            attachmentCount: 0
          }
        ],
        total: 3
      }
    })
  })
}

export const createArchive = (data) => {
  console.log('调用创建档案API:', data)
  return apiClient.post('/archives', data).catch(error => {
    console.error('创建档案API失败:', error)
    return Promise.resolve({ id: Date.now() })
  })
}

export const updateArchive = (id, data) => {
  console.log('调用更新档案API:', id, data)
  return apiClient.put(`/archives/${id}`, data).catch(error => {
    console.error('更新档案API失败:', error)
    return Promise.resolve({ success: true })
  })
}

export const deleteArchive = (id) => {
  console.log('调用删除档案API:', id)
  return apiClient.delete(`/archives/${id}`).catch(error => {
    console.error('删除档案API失败:', error)
    return Promise.resolve({ success: true })
  })
}

export const getArchiveById = (id) => {
  console.log('调用获取档案详情API:', id)
  return apiClient.get(`/archives/${id}`).catch(error => {
    console.error('获取档案详情API失败:', error)
    return Promise.resolve({
      data: {
        id: id,
        documentName: '模拟档案',
        documentType: 1,
        documentDate: '2024-01-15',
        documentPurpose: '模拟用途',
        documentSource: '模拟来源',
        remark: '模拟档案描述',
        attachmentIds: '1,2',
        attachmentCount: 2
      }
    })
  })
}

export const checkArchiveNumber = (archiveNumber, excludeId = null) => {
  console.log('调用检查档案编号API:', archiveNumber, excludeId)
  return apiClient.get('/archives/check', {
    params: { archiveNumber, excludeId }
  }).catch(error => {
    console.error('检查档案编号API失败:', error)
    return Promise.resolve({ data: true })
  })
}

// 按分类查询档案
export const getArchivesByCategory = (categoryId, params) => {
  console.log('调用按分类查询档案API:', categoryId, params)
  return apiClient.get(`/archives/category/${categoryId}`, { params }).catch(error => {
    console.error('按分类查询档案API失败:', error)
    return Promise.resolve({
      data: {
        content: [
          {
            id: 1,
            documentName: '分类档案',
            documentType: 1,
            documentDate: '2024-01-15',
            documentPurpose: '分类用途',
            documentSource: '分类来源',
            remark: '分类档案描述',
            attachmentIds: '1,2',
            attachmentCount: 2
          }
        ],
        total: 1
      }
    })
  })
}

// 搜索档案
export const searchArchives = (params) => {
  console.log('调用搜索档案API:', params)
  return apiClient.get('/archives/search', { params }).catch(error => {
    console.error('搜索档案API失败:', error)
    return Promise.resolve({
      data: {
        content: [],
        total: 0
      }
    })
  })
}

// 导出档案
export const exportArchives = (params) => {
  console.log('调用导出档案API:', params)
  return apiClient.get('/archives/export', {
    params,
    responseType: 'blob'
  }).catch(error => {
    console.error('导出档案API失败:', error)
    return Promise.resolve({
      data: new Blob(['模拟导出数据'], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
    })
  })
}

// 默认导出
export default {
  getList: getArchives,
  create: createArchive,
  update: updateArchive,
  delete: deleteArchive,
  getById: getArchiveById,
  checkArchiveNumber,
  getByCategory: getArchivesByCategory,
  search: searchArchives,
  export: exportArchives
}